import Vue from "vue";
import Router from "vue-router";
import Home from "./view/home.vue";
import About from "./view/about.vue";
import Login from "./view/login.vue";

Vue.use(Router);

const router = new Router({
    mode: "history",
    base: process.env.BASE_URL,
    routes:[
        {
            path: "/",
            name: "home",
            component: Home
        },{
            path: "/about",
            name: "about",
            meta: {
                auth: true
            },
            component:About
        },{
            path: "/login",
            name: "login",
            component: Login
        }
    ]
})


router.beforeEach((to, from, next) => {
    if(to.meta.auth) {
        // 需要登录
        const token = localStorage.getItem("token");
        if(token) {
            next()
        } else{
            next({
                path:"/login",
                query:{
                    redirect: to.path
                }
            })
        }
    }else{
        // 没有auth 属性的不需要验证
        next();
    }
})

// 最后暴露方法

export default router;